package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class ProductFrame extends JFrame {
	private JPanel pnlTop = null;
	private JLabel lblProductName = null;
	private JLabel lblProductDesc = null;
	private JLabel lblProductStatus = null;
	private JButton btnAdd = null;
	private JButton btnDelete = null;
	private JButton btnUpdate = null;

	private JScrollPane panTable = null;
	private JTable tblProduct = null;
	private ProductTableModel tmdProduct = null;

	private ArrayList lstProduct = null;

	public ProductTableModel getTmdProduct() {
		if (null == tmdProduct) {
			tmdProduct = new ProductTableModel(lstProduct);
			return tmdProduct;
		}
		return tmdProduct;
	}

	public JTable getTblProduct() {
		if (null == tblProduct) {
			tblProduct = new JTable(getTmdProduct());
			tblProduct.setEnabled(true);
			tblProduct.setRowSelectionAllowed(true);
			return tblProduct;
		}
		return tblProduct;
	}

	public JScrollPane getPanTable() {
		if (null == panTable) {
			panTable = new JScrollPane();
			panTable.setViewportView(getTblProduct());
			return panTable;
		}
		return panTable;
	}

	public JPanel getPnlTop() {
		if (null == pnlTop) {
			pnlTop = new JPanel();
			pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER));
			pnlTop.add(getLblProductName());
			pnlTop.add(getLblProductDesc());
			pnlTop.add(getLblProductStatus());
			pnlTop.add(getBtnAdd());
			pnlTop.add(getBtnDelete());
			pnlTop.add(getBtnUpdate());
			return pnlTop;
		}
		return pnlTop;
	}

	public JLabel getLblProductName() {
		if (null == lblProductName) {
			lblProductName = new JLabel("Product Name");
			return lblProductName;
		}
		return lblProductName;
	}

	public JLabel getLblProductDesc() {
		if (null == lblProductDesc) {
			lblProductDesc = new JLabel("Product Description");
			return lblProductDesc;
		}
		return lblProductDesc;
	}

	public JLabel getLblProductStatus() {
		if (null == lblProductStatus) {
			lblProductStatus = new JLabel("Status");
			return lblProductStatus;
		}
		return lblProductStatus;
	}

	public JButton getBtnAdd() {
		if (null == btnAdd) {
			btnAdd = new JButton("添加");
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					addProductBean();

				}

			});
			return btnAdd;
		}
		return btnAdd;
	}

	public JButton getBtnDelete() {
		if (null == btnDelete) {
			btnDelete = new JButton("删除");
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					deleteProduct();

				}

			});
			return btnDelete;
		}
		return btnDelete;
	}

	public JButton getBtnUpdate() {
		if (null == btnUpdate) {
			btnUpdate = new JButton("更新");
			btnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					updateProduct();

				}

			});
			return btnUpdate;
		}
		return btnUpdate;
	}

	public void addProductBean() {
		ProductBean p = new ProductBean();
		getTmdProduct().addList(getTblProduct().getSelectedRow(), p);
	}

	public void updateProduct() {
		ProductBean p = getTmdProduct().getList(
				getTblProduct().getSelectedRow());
		if (p != null) {
			getTmdProduct().updateList(getTblProduct().getSelectedRow(), p);
		}
	}

	public void deleteProduct() {
		getTmdProduct().deleteList(getTblProduct().getSelectedRow());
	}

	public void initData() {
		lstProduct = new ArrayList();
	}

	public ProductFrame() {
		initData();
		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		c.add(getPnlTop(), BorderLayout.NORTH);
		c.add(getPanTable(), BorderLayout.CENTER);
		this.setSize(new Dimension(800, 600));
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new ProductFrame();
	}

}
